//验证姓名1。
function checkName1(value){
	if(isOtherName(value)){
		if(CheckAllCnStr(value)){
			return true;
		}else{
			if(CheckEnStr(value)){
				return true;
			}
			else{
				return false;
			}
		}
	}else{
		return false;
	}		
}

//验证姓名。
function checkName(value){
	var name = IsEmpty(value);
	if(name==false){
		return false;
	}else{
		if(isOtherName(name)){
			if(CheckAllCnStr(name)){
				return true;
			}else{
				if(CheckEnStr(name)){
					return true;
				}
				else{
					return false;
				}
			}
		}else{
			return false;
		}		
	}
}

//验证拼音/英文姓名。
function checkEnName(value){
	var name = IsEmpty(value);
	if(name==false){
		return false;
	}else{
		if(CheckEnStr(name)){
			return true;
		}else{
			return false;
		}		
	}
}

//验证生日
function checkbirthday(birthday){
var sdate = IsEmpty(birthday);
	if(sdate==false){
		return false;
	}else{
		if(IsDate2(sdate))
			{return true;}
		else
			{return false;}
	}
}


function checkAllInfo(){
   var res = false;
   	  res=checkIdExist();
   	 // alert("idexist="+res);
   if(!res)
   	   return res;
     res=checkAllPsg();
     //alert("checkAllPsg="+res);
   if(!res)
   	   return res;
   		res=checkLinkman();
   	  //alert("checkLinkman="+res);
   if(!res)
   	   return res;
   /*	res=checkpay();
   if(!res)
   	   return res;*/
   if($("").attr(".opbcbx .radio")){
   	res=checkdlv();
   if(!res)
   	   return res;
   }
   	return res;
}

function checkAllPsg(){
	var res = true;
	var paperType="";
	var theFocus = "";
    $(".psgType").each(function(){
    	var cur_psgType = $.trim($(this).val());
    	var psgType_id = $(this).attr("id");
        if("0"==cur_psgType){
        	$(this).parent().parent().parent().children("ul").each(function(){
        		$(this).children().each(function(){
        			$(this).children().each(function(){
        				if($(this).attr("name")=="psgName")
        				{
        					var cur_psgName = $.trim($(this).val());
        					if(checkName(cur_psgName)){
        					}else{
        						$(this).after(errMsg("姓名格式不正确，正确的格式如下：张三或Zhang/San"));

								if(theFocus==""){
									theFocus = psgType_id;
								}
        						res=false;
        					}
        				}
        				if($(this).attr("name")=="paperType")
        				{	
        					paperType=$(this).val();
        				}
        				if($(this).attr("name")=="paperId")
        				{
        					var paperId = $.trim($(this).val());
							paperId = paperId.replace(/\s+/g,"");
							if (paperType == "none" || paperType == "" || paperType == "None") {
								//$(this).after(errMsg("请选择证件类型"));
								res=false;
							}else{
								$(this).next().remove();
							}
							if (paperId.length < 2 || paperId.length > 20) {
								$(this).after(errMsg("请正确输入证件号码"));
								if(theFocus==""){
									theFocus = $(this).attr("id");
								}
								res=false;
							}else{
								$(this).next().remove();
							}
							if (paperType == "身份证") {
								if(DocumentNum(paperId) == false){
									$(this).after(errMsg("请正确输入身份证号码"));
									if(theFocus==""){
										theFocus = $(this).attr("id");
									}
									res=false;
								}else{
									$(this).next().remove();
								}
							}else{
								if(DocumentNum4Others(paperId) == false){
									$(this).after(errMsg("请输入正确的证件号码"));
									if(theFocus==""){
										theFocus = $(this).attr("id");
									}
									res=false;
								}else{
									$(this).next().remove();
								}
							}
        				}
        			
        			});
        		});	
        	}
        	);
        }else if("1"==cur_psgType){
        	$(this).parent().parent().parent().children("ul").each(function(){
        	  $(this).children().each(function(){
        			$(this).children().each(function(){
        				if($(this).attr("name")=="psgName")
        				{
        					if(checkName($(this).val())){	
        						$(this).next().remove();
        					}else{
        						$(this).after(errMsg("姓名格式不正确，正确的格式如下：张三或Zhang/San"));
								if(theFocus==""){
									theFocus = psgType_id;
								}
        						res=false;
        					}
        				}
//        				if($(this).attr("name")=="accompanyName")
//        				{
//        					if(isEmpty($(this).val())){
//        						$(this).next().remove();
//        					}else{
//        						$(this).after(errMsg("陪护人不能为空。"));
//        						res=false;
//        					}
//       				}
        				if($(this).attr("name")=="birthday")
        				{
        						if(IsDate2($(this).val())){
        							$(this).next().remove();
        							var age=BirthdayCheckYearOld ($(this).val(), takeoff);
        							if(age==2){
        								$(this).next().remove();
        							}else{
        								$(this).after(errMsg("您的年纪不符合。"));
										if(theFocus==""){
											theFocus = $(this).attr("id");
										}
        								res=false;
        							}
        						}else{
        							$(this).after(errMsg("您输入的日期格式不正确，正确格式为：YYYY-MM-DD"));
									if(theFocus==""){
										theFocus = $(this).attr("id");
									}
        							res=false;
        						}
        					
        				}
        			
        			});
        		});	
        	}
        	
        	);
        }
       /* else if("3"==cur_psgType){
        	$(this).parent().parent().parent().children("ul").each(function(){
        	$(this).children().each(function(){
        			$(this).children().each(function(){
        				if($(this).attr("name")=="psgName")
        				{
        					if(checkName($(this).val())){
        						$(this).next().remove();
        					}else{
        						$(this).after(errMsg("姓名格式不正确，正确的格式如下：张三"));
								if(theFocus==""){
									theFocus = psgType_id;
								}
        						res=false;
        					}
        				}
        				if($(this).attr("name")=="psgEnName")
        				{
        					if(checkEnName($(this).val())){
        						$(this).next().remove();
        					}else{
        						$(this).after(errMsg("姓名格式不正确，正确的格式为:Zhang/San"));
								if(theFocus==""){
									theFocus = $(this).attr("id");
								}
        						res=false;
        					}
        				}
//        				if($(this).attr("name")=="accompanyName")
//        				{
//        					if(isEmpty($(this).val())){
//        						$(this).next().remove();
//        					}else{
//        						$(this).after(errMsg("陪护人不能为空。"));
//        						res=false;
//        					}
//        				}
        				/*if($(this).attr("name")=="birthday")
        				{
       						if(IsDate2($(this).val())){
       							$(this).next().remove();
       							var age=BirthdayCheckYearOld ($(this).val(), takeoff);
       							if(age==3){
       								$(this).next().remove();
       							}else{
       								$(this).after(errMsg("您的年纪不符合。"));
									if(theFocus==""){
										theFocus = $(this).attr("id");
									}
       								res=false;
       							}
       						}else{
       							$(this).after(errMsg("您输入的日期格式不正确，正确格式为：YYYY-MM-DD"));
								if(theFocus==""){
									theFocus = $(this).attr("id");
								}
       							res=false;
       						}
        				}
        			
        			});
        		});
        	}
        	
        	);
        } */
	}); 
//	if(!res){
//		$("html,body").animate({scrollTop: $("#"+theFocus).offset().top-50}, 500);
//	}	
	return res;
}
//证件号码重复（TODO）
function checkIdExist(){
		var flag = true;
		var ID_map = new Map();
		$(".psgType").each(function(){
			var cur_ram_id = $(this).attr("id").replace("psgType","");
	    	var cur_psgType = $.trim($(this).val());
	    	
	        if("1"==cur_psgType){
	        	var tmp_paper_type_id = "paperType" + cur_ram_id;
	        	var tmp_paper_id_id = "paperId" + cur_ram_id;
	        	
	        	var tmp_paper_type_value = $("#"+tmp_paper_type_id).val();
	        	var tmp_paper_id_value = $("#"+tmp_paper_id_id).val();
	        	if(tmp_paper_type_value!=null && tmp_paper_type_value!="" && tmp_paper_id_value!=null && tmp_paper_id_value!=""){
	        		var t_flag = false;
	        		var t_key = tmp_paper_type_value+tmp_paper_id_value;
	        		var t_value = tmp_paper_id_id;
	        		for(var k=0;k<ID_map.size();k++){
						var keys = ID_map.keys();
	        			if(t_key ==keys[k]){
	        				t_flag = true;
	        			}
	        		}
	        		if(!t_flag){
	        			ID_map.put(t_key,t_key);
	        		}else{
    					$("#"+tmp_paper_id_id).after(errMsg("您输入的证件号码重复"));
    					$("html,body").animate({scrollTop: $("#"+tmp_paper_id_id).offset().top}, 500);
    					flag = false;
	        		}
	        	}
	        }
		});
		return flag;
}

function checkLinkman(){
	var res = true;
	var theFocus = "";
	//联系人姓名校验
	var tkMemberInfoHis_orderLinkman=$("#tkMemberInfoHis_orderLinkman").val();
	if (tkMemberInfoHis_orderLinkman==null || tkMemberInfoHis_orderLinkman==""){
		    $("#tkMemberInfoHis_orderLinkman").after(errMsg("姓名格式不正确，正确的格式如下：张三或Zhang/San"));
			if(theFocus==""){
				theFocus = "tkMemberInfoHis_orderLinkman";
			}
		    res=false;
	}else{
		$("#tkMemberInfoHis_orderLinkman").next().remove();
	}
	if (!checkName(tkMemberInfoHis_orderLinkman)){
		    $("#tkMemberInfoHis_orderLinkman").after(errMsg("姓名格式不正确，正确的格式如下：张三或Zhang/San"));
			if(theFocus==""){
				theFocus = "tkMemberInfoHis_orderLinkman";
			}
		    res=false;
	}else{
		$("#tkMemberInfoHis_orderLinkman").next().remove();
	}
	//联系人手机校验
	var tkMemberInfoHis_cellphone=$("#tkMemberInfoHis_cellphone").val();
	if (tkMemberInfoHis_cellphone==null || tkMemberInfoHis_cellphone==""){
		    $("#tkMemberInfoHis_cellphone").after(errMsg("您输入的手机号码为空，请输入11位的手机号码"));
			if(theFocus==""){
				theFocus = "tkMemberInfoHis_cellphone";
			}
		     res=false;
	}else{
		$("#tkMemberInfoHis_cellphone").next().remove();
	}
	if (!IsMobileNoAlert(tkMemberInfoHis_cellphone)){
		    $("#tkMemberInfoHis_cellphone").after(errMsg("您输入的手机号码有误，请输入11位的手机号码"));
			if(theFocus==""){
				theFocus = "tkMemberInfoHis_cellphone";
			}
		     res=false;
	}else{
		$("#tkMemberInfoHis_cellphone").next().remove();
	}
	//联系人电话校验(区号)
	return res;
}


/*function checkpay(){
	var flag = false;
	var gflag = false;
	var theFocus = "";
	$(".radiozf").each(function(){
		var curStatus = $(this).attr("checked");
		///alert("curStatus:"+curStatus);
		if(curStatus){
			flag=true;
			var ptype=$(this).val();
			if("0000300003"==ptype){				
				$(".gateidcss").each(function(){
					var gateid = $(this).attr("checked");					
					if(gateid){
						gflag=true;						
					}
				});
				if(!gflag){
					$("#show_payBank").after("<div class=\"errorBottomPrompt onWebn\"><div class=\"pmt\"><p>请选择网上银行</p><div class=\"inArr\"></div></div></div>");
					if(theFocus==""){
						theFocus = "zffstype";
					}
				}
			}
		}
	});
	if(flag){
		if(getCurPayType2()=="0000300003"){
			$(".payTypeError").hide();
			if(!gflag){
				flag = false;
			}
		}
	}else{
			var isPayByFixedWithoutCredit = false;
			var sub_pay_fee = 0;	//混合支付，信用卡支付金额（默认为0）
				
			if($(".useBigCouponCard").attr("checked")){
				
				var total_pay_fee = parseInt($('#total_fee').html().replace('¥',''));
				sub_pay_fee = total_pay_fee - parseInt(remainCouponFee);
				var fix_show_contents = "";
				if(sub_pay_fee>0){
					
				}else{
					isPayByFixedWithoutCredit = true;
				}
			}	
			
			if(isPayByFixedWithoutCredit){
				//是混合支付，但全由优旅卡支付
				flag = true;
				//alert('222222222');
			}else{
				//alert('111111111');
				$(".payTypeError").show();
				if(theFocus==""){
					theFocus = "zffstype";
				}
			}
			

			//return false;
	}
	if(!flag){
		//$("html,body").animate({scrollTop: $("#"+theFocus).offset().top}, 500);
	}
	return flag;
}*/
function checkdlv(){
	var flag = false;
	var subFlag = true;
	var theFocus = "";
	$(".opbcbx .radio").each(function(){
		var curStatus = $(this).attr("checked");
		if(curStatus){
			flag=true;
			var cur_deliveryType_value = $(this).val();
			if("0000200004"==cur_deliveryType_value){				//邮寄行程单
				var orderInfo_delivery_recipients = $("#orderInfo_delivery_recipients").val();
				var selectProvince = $("#selectProvince").val();
				var city = $("#city").val();
				var county = $("#county").val();
				var orderInfo_delivery_mailAddress = $("#orderInfo_delivery_mailAddress").val();
				var orderInfo_delivery_zipcode = $("#orderInfo_delivery_zipcode").val();
				var orderInfo_delivery_recipientsTel = $("#orderInfo_delivery_recipientsTel").val();
				
				if(checkName(orderInfo_delivery_recipients)){
					$("#orderInfo_delivery_recipients").next().remove();
				}else{
					$("#orderInfo_delivery_recipients").after(errMsg("姓名格式不正确，正确的格式如下：张三或Zhang/San"));
					if(theFocus==""){
						theFocus = "orderInfo_delivery_recipients";
					}
					 subFlag = false;
				}
				if(orderInfo_delivery_mailAddress!=null && orderInfo_delivery_mailAddress!=""){
					$("#orderInfo_delivery_mailAddress").next().remove();
				}else{
					$("#orderInfo_delivery_mailAddress").after(errMsg("请填写详细地址"));
					if(theFocus==""){
						theFocus = "orderInfo_delivery_mailAddress";
					}
					 subFlag = false;
				}
				if(orderInfo_delivery_zipcode!=null && orderInfo_delivery_zipcode!=""){
					$("#orderInfo_delivery_zipcode").next().remove();
				}else{
					$("#orderInfo_delivery_zipcode").after(errMsg("请填写地区区号"));
					if(theFocus==""){
						theFocus = "orderInfo_delivery_zipcode";
					}
					 subFlag = false;
				}
				if(orderInfo_delivery_recipientsTel!=null && orderInfo_delivery_recipientsTel!="" && IsMobileNoAlert(orderInfo_delivery_recipientsTel)){
					$("#orderInfo_delivery_recipientsTel").next().remove();
				}else{
					$("#orderInfo_delivery_recipientsTel").after(errMsg("请正确填写收件人联系电话"));
					if(theFocus==""){
						theFocus = "orderInfo_delivery_recipientsTel";
					}
					 subFlag = false;
				}
			}
		}
	});
	if(!subFlag){
		flag = false;
		$(".deliveryTypeError").hide();
	}else{
		if(!flag){
			$(".deliveryTypeError").show();
			if(theFocus==""){
				theFocus = "delivery_error_div";
			}
			//return false;
		}else{
			$(".deliveryTypeError").hide();
		}
	}
//	if(!flag){
//		$("html,body").animate({scrollTop: $("#delivery_error_div").offset().top}, 500);
//	}
	return flag;
}

///base valid++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/*验证email格式*/
function IsEmail(String){
	var testreg=/^[a-zA-Z0-9_+.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$/;
	 
	if(testreg.test(String)){
		return true;
	}else
	   return false;
}
/*验证生日格式*/
function IsDate2(birthday){
	var testreg=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
	if(testreg.test(birthday)){
		return true;
	}else{
	   return false;
	}
}
/*验证字符串长度*/
function Islargermore(String){
	
	 
	if(String.length<140){
		return true;
	}else
	   return false;
}
/*字符串不能为空 */
function IsEmpty (string) 
{
	var str = string.replace(/\s+/g,"");
	if (str == '') return false;
	else return str;
}

/*字符串不能为空 返回boolean */
function isEmpty (string) 
{
	
	if (string == '') return false;
	else return true;
}

/*字符串是否是数字 */
function IsNumber (string)
{
	var number = IsEmpty(string);
	if (number == false) return false;
	if(/^[0-9]+$/.test(number)) return true;
	else return false;	
}

/* 手机号码验证  */
function IsMobile (string) 
{
	var mobile = IsEmpty(string);
	if (mobile == false) return false;
	//var pattern = /^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/;
	var pattern = /^(13[0-9]|15[0|3|6|7|8|9]|18[2|8|9])\d{8}$/; 
	if(pattern.test(mobile)) return true;
	else{
	alert("手机号码非法，请输入真实的号码。");
	 return false;
	 }
}

/* 手机号码验证,无alert  */
function IsMobileNoAlert (string) 
{
	var mobile = IsEmpty(string);
	if (mobile == false) return false;
	if(/^(13[0-9]|15[0|1|2|3|5|6|7|8|9]|18[2|6|8|9])\d{8}$/.test(mobile)) return true;
	else{
	 return false;
	 }
}


/*号码验证包括手机号、电话号和传真号*/
function isTel(str){
	var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
	if(str!="")   
        {   
            if(!pattern.exec(str))   
            {   
			return false;
            }
        return true;       
        }   
}  

function exist(id) {
    var s = document.getElementById(id);
    if(s)  return true;
    else  return false;
}


/*姓名是否含有特殊符号 */
function isOtherName (strname) 
{
	var GuestName = IsEmpty(strname);
	if (GuestName == false) return false;
	var ErrStr;
	var ErrChar;
	var ArryErrStr;	
	ErrChar="!@#$%^&*()_+|-=\\~`;'[]{}\"':,;.<>?～！@#￥％^&×（）……＋|－＝、〔〕｛｝：“；‘《》？，。、0123456789";
	ErrStr="虚拟,傻冒,先生,小姐,代订,你好,哈哈,服务,孙子,龟儿子";
	
	ArryErrStr = ErrStr.split(",");
	
	for(var k=0;k<GuestName.length;k++){ //是否含有非法字符
		if(ErrChar.indexOf(GuestName.charAt(k))>-1) return false;
	}
	
	if ( ErrStr!=null && ErrStr!="" ){ //是否含有非法字符串
		for (k=0; k<ArryErrStr.length; k++){
			if (GuestName.indexOf(ArryErrStr[k])>-1) return false;
		}
	}
	return true;
}

/*姓名是否包含中文字符*/
function CheckCnStr(name)
{
    var str = IsEmpty(name);
    if (str == false) return false;
	var filter = /[^\x00-\xff]/gi;
    if (filter.test(str)) return true;
    return false;
}

/*验证是否全部包含中文字符*/
function CheckAllCnStr(name)
{
    var str = IsEmpty(name);
    if (str == false) return false;
	var filter = /[^\u4e00-\u9fa5]/;
    if (!filter.test(str)) return true;
    return false;
}

function CheckEnStr(name)
{
		if (/^[a-zA-Z\/]+$/.test(name)) {
			var name_ofindex_num = name.indexOf('/');
			var name_oflast_num = name.lastIndexOf('/');
			if (name_ofindex_num <= 0 || name_ofindex_num != name_oflast_num || name_ofindex_num == (name.length - 1)) {
				return false;
			}
		} 
		return true;
}

/*由出生日期、日期(2010-3-23)验证年龄
 * 成人大于12岁，返回1
 * 儿童大于2岁小于12岁，返回2
 * 婴儿小于两岁，返回3
 **/
function BirthdayCheckYearOld(birthday, takeoff)
{
	var birth = birthday.split('-');
	var tmp = takeoff.split('-');
	var age = tmp[0] - birth[0];
	if ((birth[1] == (tmp[1] + 1)) && (birth[2] <= parseInt(tmp[2]))) age = age; 
	else if (birth[1] <= tmp[1]) age = age;
	else age = age - 1; 
	if (age < 2) return 3;
	else if (age < 12) return 2;
	else return 1;
}
 function Map() {      
  var struct = function(key, value) {      
   this.key = key;      
   this.value = value;      
  }      
  var keys = function(){ 
   var key_values = new Array();      
   for (var i = 0; i < this.arr.length; i++) {      
	key_values[i] = this.arr[i].key;     
   } 	  
    return key_values;      
  }   
  var put = function(key, value){      
   for (var i = 0; i < this.arr.length; i++) {      
    if ( this.arr[i].key === key ) {      
     this.arr[i].value = value;      
     return;      
    }      
   }      
    this.arr[this.arr.length] = new struct(key, value);      
  }      
        
  var get = function(key) {      
   for (var i = 0; i < this.arr.length; i++) {      
    if ( this.arr[i].key === key ) {      
      return this.arr[i].value;      
    }      
   }      
   return null;      
  }      
        
  var remove = function(key) {      
   var v;      
   for (var i = 0; i < this.arr.length; i++) {      
    v = this.arr.pop();      
    if ( v.key === key ) {      
     continue;      
    }      
    this.arr.unshift(v);      
   }      
  }      
        
  var size = function() {      
   return this.arr.length;      
  }      
        
  var isEmpty = function() {      
   return this.arr.length <= 0;      
  }      
  this.keys = keys; 
  this.arr = new Array();      
  this.get = get;      
  this.put = put;      
  this.remove = remove;      
  this.size = size;      
  this.isEmpty = isEmpty;      
 }      
